package com.example.mybatisplus.mapper;

import com.example.mybatisplus.model.domain.Admins;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus.model.domain.Violations;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lxp
 * @since 2024-07-16
 */
@Repository
public interface AdminsMapper extends BaseMapper<Admins> {

    @Select("SELECT * FROM Admins")
    public List<Admins> getAllAdmins();


    @Insert("INSERT INTO admins (login_name, permission_level, token)\n"+
           "VALUE (#{loginName},#{permissionLevel},#{token})")
    public void insertAll(Admins admins);


    @Update("UPDATE admins SET login_name = #{loginName}, permission_level = #{permissionLevel} WHERE id = #{id}")
    public void update(Admins admins);

    @Update("UPDATE admins SET is_enabled = false, is_deleted = true WHERE id = #{id}")
    public void delete(Admins admins);

    @Select("SELECT * FROM admins WHERE login_name LIKE CONCAT('%', #{loginName}, '%') AND permission_level = #{permissionLevel}")
    List<Admins> selectByLoginNameAndPermissionLevel(Admins admins);

    @Select("SELECT * FROM admins WHERE login_name LIKE CONCAT('%', #{loginName}, '%')")
    List<Admins> selectByLoginName(String loginName);

    @Select("SELECT * FROM admins WHERE permission_level = #{permissionLevel}")
    List<Admins> selectByPermissionLevel(Integer permissionLevel);

    default List<String> getNamesByIds(List<Integer> ids) {
        List<String> names = new ArrayList<>();
        for (Integer id : ids) {
            String name = getNameById(id.toString());
            names.add(name);
        }
        return names;
    }

    @Select("SELECT login_name FROM admins WHERE id = #{id}")
    String getNameById(String id);

}
